package com.lenovo.lcdm.dcm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.lenovo.lcdm.dcm.dto.CtoCVsDto;
import com.lenovo.lcdm.dcm.dto.DummyConfigurableModelCatagoryDto;
import com.lenovo.lcdm.dcm.entity.DummyConfigurableModelCategory;
import com.lenovo.lcdm.dcm.mapper.DummyConfigurableModelCategoryMapper;
import com.lenovo.lcdm.dcm.service.DummyConfigurableModelCategoryService;
import com.lenovo.lcdm.dcm.service.DummyConfigurableModelProductFeaturesDescService;
import com.lenovo.lcdm.dcm.vo.DummyConfigurableModelCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunby1
 * @description 针对表【plm_dummy_configurable_model_category】的数据库操作Service实现
 * @createDate 2023-12-27 15:29:12
 */
@Service
public class DummyConfigurableModelCategoryServiceImpl extends ServiceImpl<DummyConfigurableModelCategoryMapper, DummyConfigurableModelCategory>
        implements DummyConfigurableModelCategoryService {

    private final DummyConfigurableModelProductFeaturesDescService dummyConfigurableModelProductFeaturesDescService;

    @Autowired
    public DummyConfigurableModelCategoryServiceImpl(DummyConfigurableModelProductFeaturesDescService dummyConfigurableModelProductFeaturesDescService) {
        this.dummyConfigurableModelProductFeaturesDescService = dummyConfigurableModelProductFeaturesDescService;
    }


    @Override
    public List<DummyConfigurableModelCategoryVo> getCategoryVos(Long dcmId) {
        List<DummyConfigurableModelCategory> categories = lambdaQuery().eq(DummyConfigurableModelCategory::getDcmId, dcmId).orderByAsc(DummyConfigurableModelCategory::getCategoryName).list();
        return categories.stream().map(dummyConfigurableModelCategory -> {
            DummyConfigurableModelCategoryVo dummyConfigurableModelCategoryVo = new DummyConfigurableModelCategoryVo();
            BeanUtil.copyProperties(dummyConfigurableModelCategory, dummyConfigurableModelCategoryVo);
            dummyConfigurableModelCategoryVo.setDummyConfigurableModelProductFeaturesDescVos(dummyConfigurableModelProductFeaturesDescService.getFeatures(dummyConfigurableModelCategory.getId()));
            return dummyConfigurableModelCategoryVo;
        }).toList();
    }

    @Override
    @Transactional
    public void delCategoriesByDcmId(Long id) {
        LambdaQueryChainWrapper<DummyConfigurableModelCategory> dcmcQueryChainWrapper = lambdaQuery().eq(DummyConfigurableModelCategory::getDcmId, id);
        List<DummyConfigurableModelCategory> dummyConfigurableModelCategories = dcmcQueryChainWrapper.list();
        List<Long> ids = dummyConfigurableModelCategories.stream().map(DummyConfigurableModelCategory::getId).toList();
        dummyConfigurableModelProductFeaturesDescService.delFeatureByCategoryIds(ids);
        removeBatchByIds(ids);
    }

    @Override
    public void copyCategoriesByDcmId(Integer oldId, Integer newId) {
        List<DummyConfigurableModelCategory> dummyConfigurableModelCategories = lambdaQuery().eq(DummyConfigurableModelCategory::getDcmId, oldId).list();
        dummyConfigurableModelCategories.forEach(dcmc -> {
            DummyConfigurableModelCategory oldDcmc = BeanUtil.copyProperties(dcmc, DummyConfigurableModelCategory.class, "id, dcmId");
            Long oldDcmcId = oldDcmc.getId();
            dcmc.setDcmId(Long.valueOf(newId));
            save(dcmc);
            dummyConfigurableModelProductFeaturesDescService.copyFeatureByCategoryId(oldDcmcId, dcmc.getId());
        });
    }

    @Override
    public List<DummyConfigurableModelCatagoryDto> getCategoryDtos(Long id) {
        List<DummyConfigurableModelCategory> categories = lambdaQuery().eq(DummyConfigurableModelCategory::getDcmId, id).list();
        return Lists.newArrayList(categories.stream().map(dummyConfigurableModelCategory -> {
            DummyConfigurableModelCatagoryDto dummyConfigurableModelCatagoryDto = new DummyConfigurableModelCatagoryDto();
            BeanUtil.copyProperties(dummyConfigurableModelCategory, dummyConfigurableModelCatagoryDto);
            dummyConfigurableModelCatagoryDto.setCategory(dummyConfigurableModelCategory.getCategoryName());
            if (ObjUtil.isEmpty(dummyConfigurableModelCategory.getFullCv())) {
                dummyConfigurableModelCatagoryDto.setFullCvs(JSONUtil.toList(JSONUtil.toJsonStr(dummyConfigurableModelCategory.getFullCv()), CtoCVsDto.class));
            } else {
                dummyConfigurableModelCatagoryDto.setFullCvs(Lists.newArrayList());
            }
            dummyConfigurableModelCatagoryDto.setDummyConfigurableModelFeatureDtos(dummyConfigurableModelProductFeaturesDescService.getFeatureDtos(dummyConfigurableModelCategory.getId()));
            return dummyConfigurableModelCatagoryDto;
        }).toList());
    }

    @Override
    @Transactional
    public void saveByDcmId(Long id, List<DummyConfigurableModelCatagoryDto> dummyConfigurableModelCatagoryDtos) {
        List<DummyConfigurableModelCategory> categories = dummyConfigurableModelCatagoryDtos.stream().map(dummyConfigurableModelCatagoryDto -> {
            DummyConfigurableModelCategory dummyConfigurableModelCategory = new DummyConfigurableModelCategory();
            dummyConfigurableModelCategory.setCategoryName(dummyConfigurableModelCatagoryDto.getCategory());
            dummyConfigurableModelCategory.setFullCv(dummyConfigurableModelCatagoryDto.getFullCvs());
            dummyConfigurableModelCategory.setDcmId(id);
            return dummyConfigurableModelCategory;
        }).toList();
        saveBatch(categories);
        Map<String, List<DummyConfigurableModelCategory>> map = categories.stream().collect(Collectors.groupingBy(DummyConfigurableModelCategory::getCategoryName));
        List<DummyConfigurableModelCatagoryDto> list = dummyConfigurableModelCatagoryDtos.stream().peek(dummyConfigurableModelCatagoryDto -> dummyConfigurableModelCatagoryDto.setId(map.get(dummyConfigurableModelCatagoryDto.getCategory()).get(0).getId())).toList();
        dummyConfigurableModelProductFeaturesDescService.saveDcms(list);
    }

    @Override
    @Transactional
    public void delCategoriesByCategoryIds(List<Long> needRemoveCategoryIds) {
        dummyConfigurableModelProductFeaturesDescService.delFeatureByCategoryIds(needRemoveCategoryIds);
        removeBatchByIds(needRemoveCategoryIds);
    }

    @Override
    public Map<Long, List<DummyConfigurableModelCategory>> getCategoryByDcmIds(List<Long> dcmIds) {
        Map<Long, List<DummyConfigurableModelCategory>> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(dcmIds)) {
            LambdaQueryWrapper<DummyConfigurableModelCategory> qw = new LambdaQueryWrapper<>();
            qw.in(DummyConfigurableModelCategory::getDcmId, dcmIds);
            List<DummyConfigurableModelCategory> categories = list(qw);
            map = categories.stream().collect(Collectors.groupingBy(DummyConfigurableModelCategory::getDcmId));
        }
        return map;
    }

}




